<style>
    .main-login{
        width: 100%;
        height: 100vh;
        background: #ebebeb;
        font-family: "Helvetica Neue","Hiragino Sans GB","Microsoft YaHei","\9ED1\4F53",Arial,sans-serif;
        color: #222;
        font-size: 12px;
    }
    *{padding: 0;margin: 0;}
    .top_div{
        background: #008ead;
        width: 100%;
        height: 400px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .top_div h1{
        color: #FFF;
        font-size: 30px;
        margin-top: -50px;
        text-shadow: black 0 1px 2px;
    }
    .ipt{
        border: 1px solid #d3d3d3;
        padding: 10px 10px;
        width: 340px;
        border-radius: 4px;
        padding-left: 35px;
        -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
        box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
        -webkit-transition: border-color ease-in-out .3s,-webkit-box-shadow ease-in-out .3s;
        -o-transition: border-color ease-in-out .3s,box-shadow ease-in-out .3s;
        transition: border-color ease-in-out .3s,box-shadow ease-in-out .3s
    }
    .ipt:focus{
        border-color: #66afe9;
        outline: 0;
        -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
        box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)
    }
    .ipt.sm{
        width: 200px;
    }
    .u_logo{
        background: url("../../assets/images/login/username.png") no-repeat;
        padding: 10px 10px;
        position: absolute;
        top: 43px;
        left: 40px;

    }
    .p_logo{
        background: url("../../assets/images/login/password.png") no-repeat;
        padding: 10px 10px;
        position: absolute;
        top: 12px;
        left: 40px;
    }
    a{
        text-decoration: none;
    }
    .tou{
        background: url("../../assets/images/login/tou.png") no-repeat;
        width: 97px;
        height: 92px;
        position: absolute;
        top: -87px;
        left: 140px;
    }
    .left_hand{
        background: url("../../assets/images/login/left_hand.png") no-repeat;
        width: 32px;
        height: 37px;
        position: absolute;
        top: -38px;
        left: 150px;
        transition: top linear .3s, left linear .3s;
    }
    .right_hand{
        background: url("../../assets/images/login/right_hand.png") no-repeat;
        width: 32px;
        height: 37px;
        position: absolute;
        top: -38px;
        right: -64px;
        transition: top linear .3s, right linear .3s;
    }
    .initial_left_hand{
        background: url("../../assets/images/login/hand.png") no-repeat;
        width: 30px;
        height: 20px;
        position: absolute;
        top: -12px;
        left: 100px;
        transition: top linear .3s, left linear .3s;
    }
    .initial_right_hand{
        background: url("../../assets/images/login/hand.png") no-repeat;
        width: 30px;
        height: 20px;
        position: absolute;
        top: -12px;
        right: -112px;
        transition: top linear .3s, right linear .3s;
    }
    .left_handing{
        background: url("../../assets/images/login/left-handing.png") no-repeat;
        width: 30px;
        height: 20px;
        position: absolute;
        top: -24px;
        left: 139px;
    }
    .right_handinging{
        background: url("../../assets/images/login/right_handing.png") no-repeat;
        width: 30px;
        height: 20px;
        position: absolute;
        top: -21px;
        left: 210px;

    }
    .codeImg{
        width: 120px;
        height: 35px;
        float: right;
    }
    .codeImg img{
        width: 120px;
        height: 35px;
    }
</style>

<template>
    <div class="main-login" v-show="isLogin !== null">
        <div class="top_div">
            <h1>后台登录</h1>
        </div>
        <form action="" class="loginForm form clearfix validate-form" method="post">
            <div style="background: rgb(255, 255, 255); margin: -100px auto auto; border: 1px solid rgb(231, 231, 231); border-image: none; width: 400px; height: 250px; text-align: center;">
                <div style="width: 165px; height: 96px; position: absolute;">
                    <div class="tou"></div>
                    <div :class="[isPswFocus ? 'left_hand': 'initial_left_hand']"></div>
                    <div :class="[isPswFocus ? 'right_hand': 'initial_right_hand']"></div>
                </div>
                <p style="padding: 30px 0 10px; position: relative;">
                    <span class="u_logo"></span> <input class="ipt" v-model.trim="username" @keydown.enter="login" ref="username_input" type="text" name="username" placeholder="请输入用户名" value="" tabindex="1">
                </p>
                <p style="position: relative;"><span class="p_logo"></span>
                    <input class="ipt" id="password" v-model="password" @focus="pswFocus(true)" @keydown.enter="login" @blur="pswFocus(false)" type="password" ref="password_input" name="password" placeholder="请输入密码" value="" tabindex="2">
                </p>
                <p style="position: relative; margin: 10px 0">
            <span style="width: 340px; overflow: hidden; display: inline-block">
                <span class="c_logo"></span>
                <span style="float: left;"><input class="ipt sm" v-model.trim="verifyCode" tabindex="3" name="verify" maxlength="4" id="code" @keydown.enter="login" type="text" placeholder="验证码" value=""></span>
                <span class="codeImg">
                	<img :src="verifyimg" class="verifyimg" @click="refresh_verify"  alt="验证码" title="点击换一张"/>
			    </span>
            </span>
                </p>
                <div style="height: 50px; line-height: 50px; margin-top: 20px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
                    <p style="margin: 0 35px 20px 45px;">
                        <span style="float: right;">
                    <a style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;"
                       href="javascript:void(0);"
                       @click="login"
                       class="ajax-post">{{ isLogin ? '登录...' : '登录'}}</a>
                </span>
                    </p>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    import http from '../../assets/js/http';
    import userData from '../../assets/js/userData';

    export default {
        data() {
            return {
                isPswFocus: false,
                isLogin: null,
                verifyimg: this.getVerifyImg(),
                username: '',
                password: '',
                verifyCode: ''
            }
        },
        methods: {
            getVerifyImg() {
                return window.tools.getAdminUrl('verify/random') + '/r/' + (new Date()).getTime() / 1000;
            },
            pswFocus(is) {
                this.isPswFocus = is;
            },
            checkLogin() {
                if (this.username == '') {
                    this.elTip('用户名不能为空', 'error');
                    this.$refs.username_input.focus();
                    return false;
                }
                if (this.password == '') {
                    this.elTip('密码不能为空', 'error');
                    this.$refs.password_input.focus();
                    return false;
                }

                return true;
            },
            elTip(msg, type = 'success') {
                bus.$message({
                    message: msg,
                    type: type,
                    showClose: true,
                    duration: 1500
                });
            },
            login() {
                if (this.lisLogin) return;
                if (!this.checkLogin()) return;

                this.isLogin = true;

                let data = {
                    username: this.username,
                    password: this.password,
                    verfiy: this.verifyCode
                };
                let url = window.tools.getAdminUrl('index/checkLogin');
                let msg = bus.$message({
                    showClose: true,
                    message: '正在登陆...',
                    type: 'warning',
                    duration: 0
                });
                this.apiPost(url, data).then((res) => {
                    this.isLogin = false;
                    if (res.code == 1) {
                        setTimeout(() => {
                            msg.close();
                            bus.$message({
                                message: `${this.username}，登录成功`,
                                type: 'success',
                                showClose: true,
                                duration: 1500
                            });
                            window.axios.defaults.headers.sessionId = res.data.sessionid;
                            window.Lockr.set('sessionId', res.data.sessionid);
                            console.log(res.data.sessionid);
                            setTimeout(() => {
                                this.isLogin = false;
                                this.jump2Home();
                            }, 1000);
                        }, 500);
                    } else if (res.code == 0) {
                        setTimeout(() => {
                            msg.close();
                            this.refresh_verify();
                            bus.$message({
                                message: res.msg,
                                type: 'error',
                                showClose: true,
                                duration: 1500
                            });
                        }, 1000);
                    } else {
                        setTimeout(() => {
                            msg.close();
                            this.refresh_verify();
                            this.handleError(res);
                        });
                    }
                });
            },
            refresh_verify() {
                this.verifyimg = this.getVerifyImg();
            },
            //尝试获取菜单，判断登录状态
            _getUserData() {
                this.getUserData({
                    suc(res) {
                        //成功保存菜单数据到store,跳转到Home
                        this.jump2Home();
                    },
                    err() {
                        this.isLogin = false;
                    }
                });
            },
            jump2Home() {
                this.$router.push({ path: '/home' });
            }
        },
        created() {
            this._getUserData();
        },
        mounted() {
            console.log('mounted');
        },
        mixins: [http, userData]
    }
</script>